# 3.使用json库处理JSON数据
# import json


# data = {"name": "Alice", "age": 25, "city": "Beijing"}
# json_string = json.dumps(data, ensure_ascii=False)
# print(f"JSON字符串: {json_string}")

# from flask import Flask

# app = Flask(__name__)


# @app.route("/")
# def hello():
#     return "Hello, Flask!"


# if __name__ == "__main__":
#     app.run()

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def hello():
    return "Hello, FastAPI!"


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="127.0.0.1", port=8000)

# 示例1: 文件操作自动化
# def file_automation():
#     print("1. 文件操作自动化")

#     # 批量文件处理
#     print("   - 批量重命名文件")
#     print("   - 文件格式转换")
#     print("   - 目录结构整理")

#     # 模拟文件操作
#     import os
#     import shutil

#     # 创建测试目录
#     test_dir = "test_automation"
#     if not os.path.exists(test_dir):
#         os.makedirs(test_dir)

#     # 创建测试文件
#     test_files = ["file1.txt", "file2.txt", "file3.txt"]
#     for filename in test_files:
#         filepath = os.path.join(test_dir, filename)
#         with open(filepath, "w") as f:
#             f.write(f"这是{filename}的内容")

#     print(f"\n   创建了测试文件: {test_files}")

#     # 批量重命名
#     for i, filename in enumerate(test_files, 1):
#         old_path = os.path.join(test_dir, filename)
#         new_path = os.path.join(test_dir, f"renamed_{i}.txt")
#         os.rename(old_path, new_path)

#     print("   批量重命名完成")

#     # 清理测试目录
#     shutil.rmtree(test_dir)
#     print("   清理测试文件完成")


# file_automation()


# # 示例2: 系统管理自动化
# def system_automation():
#     print("\n2. 系统管理自动化")

#     # 系统监控
#     print("   - 系统资源监控")
#     print("   - 日志文件分析")
#     print("   - 性能指标收集")

#     # 模拟系统监控
#     import psutil

#     # 获取系统信息
#     cpu_percent = psutil.cpu_percent()
#     memory = psutil.virtual_memory()
#     disk = psutil.disk_usage("/")

#     print(f"\n   系统监控信息:")
#     print(f"   CPU使用率: {cpu_percent}%")
#     print(f"   内存使用率: {memory.percent}%")
#     print(f"   磁盘使用率: {disk.percent}%")


# system_automation()


# # 示例3: 网络自动化
# def network_automation():
#     print("\n3. 网络自动化")

#     # 网络请求自动化
#     print("   - 网页数据抓取")
#     print("   - API调用自动化")
#     print("   - 网络监控")

#     import urllib.request

#     url = "https://httpbin.org/get"
#     try:
#         response = urllib.request.urlopen(url)
#         data = response.read().decode("utf-8")
#         print(f"\n   网络请求成功")
#         print(f"   响应状态: {response.status}")
#         print(f"   响应长度: {len(data)} 字符")
#     except Exception as e:
#         print(f"\n   网络请求失败: {e}")


# network_automation()
